//
//  HMStatusOriginalView.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/24.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMStatusOriginalView.h"
#import "UILabel+HMLabel.h"
#import <Masonry.h>
#import "HMGlobalConst.h"
#import "HMStatusPictureView.h"

@interface HMStatusOriginalView ()

//懒加载的属性 可以放在.m文件中 .    从外界传进来的属性. 模型,必须放在.h中.
@property (nonatomic,strong) UIImageView *userImageView;

@property (nonatomic,strong) UIImageView *verifiedImageView;

@property (nonatomic,strong) UIImageView *mbrankImageView;

@property (nonatomic,strong) UILabel *screenNameLabel;

@property (nonatomic,strong) UILabel *timeLabel;

@property (nonatomic,strong) UILabel *sourceLabel;

@property (nonatomic,strong) UILabel *contentLabel;

@property (nonatomic,strong) HMStatusPictureView *pictureView;

@property (nonatomic,strong) MASConstraint *originViewBottomConstraint;
@end

@implementation HMStatusOriginalView

-(void)setStatusViewModel:(HMStatusViewModel *)statusViewModel{
    _statusViewModel = statusViewModel;
    
    self.screenNameLabel.text = statusViewModel.status.user.screen_name;
    NSString *imageUrl = statusViewModel.status.user.profile_image_url;
    
    if (imageUrl) {
        [_userImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    }
    
    self.verifiedImageView.image = statusViewModel.verifiedTypeImage;
    self.timeLabel.text = statusViewModel.timeContent;
    self.contentLabel.text = statusViewModel.status.text;
    statusViewModel.mbrankImage = self.mbrankImageView.image ;
    
    self.sourceLabel.text = statusViewModel.sourceContent;
    
    [self.originViewBottomConstraint uninstall];
    
    NSArray *picUrls = statusViewModel.status.pic_urls;
    if (picUrls.count > 0) {
        self.pictureView.hidden = NO;
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.originViewBottomConstraint = make.bottom.equalTo(_pictureView).offset(HMHomeTableViewCellMargin);
        }];
        
        _pictureView.picUrls = picUrls;
    }else{
        _pictureView.hidden = YES;
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.originViewBottomConstraint = make.bottom.equalTo(_contentLabel).offset(HMHomeTableViewCellMargin);
        }];
    
    }
    
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    
    return self;
}

-(void)setupUI{
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.userImageView];
    [self addSubview:self.verifiedImageView];
    [self addSubview:self.screenNameLabel];
    [self addSubview:self.mbrankImageView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];

    [_userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self).offset(HMHomeTableViewCellMargin);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [_verifiedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_userImageView.mas_trailing);
        make.centerY.equalTo(_userImageView.mas_bottom);
    }];
    
    [_screenNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userImageView);
        make.leading.equalTo(_userImageView.mas_trailing).offset(HMHomeTableViewCellMargin);
    }];
    
    [_mbrankImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_screenNameLabel);
        make.leading.equalTo(_screenNameLabel.mas_trailing).offset(HMHomeTableViewCellMargin);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_userImageView.mas_trailing).offset(HMHomeTableViewCellMargin);
        make.bottom.equalTo(_userImageView);
    }];
    
    [_sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_timeLabel.mas_trailing).offset(HMHomeTableViewCellMargin);
        make.bottom.equalTo(_timeLabel);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userImageView.mas_bottom).offset(HMHomeTableViewCellMargin);
        make.leading.equalTo(_userImageView);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width- 2 * HMHomeTableViewCellMargin);
        
    }];
    
    [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentLabel.mas_bottom).offset(HMHomeTableViewCellMargin);
        make.leading.equalTo(_contentLabel);
    }];
    
    
    
    //关键约束-----originView的底部,距离contentLabel的底部距离为10
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        self.originViewBottomConstraint = make.bottom.equalTo(_pictureView).offset(HMHomeTableViewCellMargin);
    }];
    
}



#pragma mark - getter

-(UIImageView *)userImageView{
    if (!_userImageView) {
        _userImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
    }
    return _userImageView;
}

-(UIImageView *)verifiedImageView{
    if (!_verifiedImageView) {
        _verifiedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_vip"]];
    }
    return _verifiedImageView;
}

-(UIImageView *)mbrankImageView{
    if (!_mbrankImageView) {
        _mbrankImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership_level1"]];
    }
    return _mbrankImageView;
}

-(UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithTextColor:[UIColor darkGrayColor] fontSize:[UIFont systemFontOfSize:14]];
    }
    return _timeLabel;
}

-(UILabel *)screenNameLabel{
    if (!_screenNameLabel) {
        _screenNameLabel = [[UILabel alloc] initWithTextColor:[UIColor darkGrayColor] fontSize:[UIFont systemFontOfSize:14]];
    }
    return _screenNameLabel;
}

-(UILabel *)sourceLabel{
    if (!_sourceLabel) {
        _sourceLabel = [[UILabel alloc] initWithTextColor:[UIColor darkGrayColor] fontSize:[UIFont systemFontOfSize:14]];
    }
    return _sourceLabel;
}

-(UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithTextColor:[UIColor darkGrayColor] fontSize:[UIFont systemFontOfSize:14]];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}


-(HMStatusPictureView *)pictureView{
    if (!_pictureView) {
        _pictureView = [[HMStatusPictureView alloc] init];
        
    }
    return _pictureView;
}



@end
